package com.qinhai.webcs.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qinhai.webcs.pojo.Product;
import com.qinhai.webcs.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @ClassName BackController
 * @Deacription
 * @Author qinhai
 * @Date 2021/4/19 16:50
 * @Version 1.0
 **/

@Controller
@RequestMapping("/back")
public class BackController {

    @Autowired
    private ProductService productService;

    @GetMapping("/main")
    public String getBackPage() {
        return "back/backPage";
    }

    @GetMapping("/product")
    public ModelAndView getBackProduct(@RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        ModelAndView modelAndView = new ModelAndView();

        Page<Product> productPage = new Page<>(pn, 10);
        Page<Product> page = productService.page(productPage, null);
        modelAndView.addObject("page", page);
        modelAndView.addObject("pn", pn);
        modelAndView.setViewName("back/backProduct");
        return modelAndView;
    }

    @GetMapping("/addPage")
    public String getAddProduct() {
        return "back/addProduct";
    }

    @GetMapping("/deleteProductById/{id}/{pn}")
    public String deleteProduct(@PathVariable("id") Integer id, @PathVariable Integer pn) {
        productService.removeById(id);
        return "redirect:/back//product?pn=" + pn;
    }

    @PostMapping("/addProd")
    public String setProduct(
            @RequestParam("name") String name,
            @RequestParam("price") Float price,
            @RequestParam("integral") Integer integral,
            @RequestParam("stock") Integer stock,
            @RequestParam("categoryleveloneId") Integer categoryleveloneId,
            @RequestPart("productImg") MultipartFile productImg
    ) throws IOException {

        String fileName = null;
        if (!productImg.isEmpty()) {
            String originalFilename = UUID.randomUUID() + productImg.getOriginalFilename();
            productImg.transferTo(new File("E:\\javaee\\graduationproject\\webcs\\src\\main\\resources\\static\\images\\" + originalFilename));
            fileName = originalFilename;
        }
        Product product = new Product();
        product.setName(name);
        product.setPrice(price);
        product.setIntegral(integral);
        product.setStock(stock);
        product.setCategoryleveloneId(categoryleveloneId);
        product.setFileName(fileName);
        productService.save(product);
        return "redirect:/back/product";
    }


    @GetMapping("/updateProduct")
    public ModelAndView getUpdate(@RequestParam("productId") Integer productId, @RequestParam("pn") Integer pn) {
        ModelAndView modelAndView = new ModelAndView();
        Product product = productService.getById(productId);
        modelAndView.addObject("product", product);
        modelAndView.addObject("pn", pn);
        modelAndView.setViewName("back/updateProductPage");
        return modelAndView;
    }

    @PostMapping("/updateById")
    public String updateById(
            @RequestParam("pn") Integer pn,
            @RequestParam("id") Integer id,
            @RequestParam("name") String name,
            @RequestParam("price") Float price,
            @RequestParam("fileName") String fileName,
            @RequestParam("integral") Integer integral,
            @RequestParam("stock") Integer stock,
            @RequestParam("categoryleveloneId") Integer categoryleveloneId,
            @RequestPart("productImg") MultipartFile productImg
    ) throws IOException {
        Product product = productService.getById(id);

        if (!name.isEmpty()) {
            product.setName(name);
        }
        if (price != null) {
            product.setPrice(price);
        }
        if (integral != null) {
            product.setIntegral(integral);
        }
        if (stock != null) {
            product.setStock(stock);
        }
        if (categoryleveloneId != product.getCategoryleveloneId()) {
            product.setCategoryleveloneId(categoryleveloneId);
        }

        String defaultFileName = null;
        if (!productImg.isEmpty()) {
            String originalFilename = UUID.randomUUID() + productImg.getOriginalFilename();
            productImg.transferTo(new File("E:\\javaee\\graduationproject\\webcs\\src\\main\\resources\\static\\images\\" + originalFilename));
            defaultFileName = originalFilename;
        } else {
            defaultFileName = fileName;
        }

        product.setFileName(defaultFileName);

        productService.updateById(product);

        return "redirect:/back//product?pn=" + pn;
    }

}
